package middleware.llvm.value;

import middleware.llvm.enums.IRType;

import java.util.List;

/**
 * 基础IR变量类型
 *
 * @author 杨凯伟
 * @date 2024/10/30
 */
public class BasicIRType extends middleware.llvm.value.IRType {
    private final IRType type;


    public BasicIRType(IRType type) {
        this.type = type;
    }

    @Override
    public IRType getType() {
        return type;
    }

    @Override
    public BasicIRType ptr(int num) {
        super.ptr(num);
        return this;
    }

    public ArrayIRType dims(List<Integer> arrayDims) {
        return new ArrayIRType(this, arrayDims);
    }

    @Override
    public String initValsToString(List<Integer> initVals) {
        return this + " " +
                (initVals.isEmpty() ? 0 : initVals.get(0));
    }


    @Override
    public BasicIRType clone() {
        var obj = new BasicIRType(this.type);
        obj.ptr(this.getPtrNum());
        return obj;
    }

    @Override
    public String toString() {
        return type.toString() + "*".repeat(getPtrNum());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof BasicIRType other) {
            return this.type.equals(other.type) && this.getPtrNum() == other.getPtrNum();
        } else {
            return false;
        }
    }
}
